﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MusicOnLine.Common;
using MusicOnLine.Models;

namespace MusicOnLine.Controllers
{
   
    public class CustControlController : Controller
    {
        MusicPlayEntities db = new MusicPlayEntities();
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public string Upload(AlbumUploadModel albumupModel)
        {
            if (ModelState.IsValid)
            {
                var count = db.Albums.Where(o => o.AlbumName == albumupModel.AlbumName && o.ArtistName == albumupModel.AlbumArtist).Count();
                if (count <= 0)
                {
                    var imgFile = Request.Files[albumupModel.TitleFile];
                    string username = HttpContext.User.Identity.Name;
                    string imgfileName = "";

                    if (imgFile != null)
                    {
                        string imgex = System.IO.Path.GetExtension(imgFile.FileName);
                        imgfileName = Guid.NewGuid().ToString() + imgex;
                        string pathImg = Server.MapPath("/AlbumImg/");
                        imgFile.SaveAs(pathImg + imgfileName);
                    }
                    foreach (var item in albumupModel.SongList)
                    {
                        var songfile = Request.Files[item.Location];
                        if (songfile != null)
                        {
                            AACInfo aacinfo = new AACInfo();
                            string ex = System.IO.Path.GetExtension(songfile.FileName);
                            string fileName = Guid.NewGuid().ToString() + ex;
                            string path = @"D:\Music\Songs\";
                            songfile.SaveAs(path + fileName);

                            aacinfo.Album = albumupModel.AlbumName;
                            aacinfo.AlbumArtist = albumupModel.AlbumArtist;
                            aacinfo.Artist = item.Artist;
                            aacinfo.Genre = albumupModel.Genre;
                            aacinfo.ReleaseDate = albumupModel.PublishDate;
                            aacinfo.Year = albumupModel.Year;
                            aacinfo.Location = fileName;
                            aacinfo.Name = item.Name;
                            aacinfo.PlayedCount = 0;
                            aacinfo.IMGPath = imgfileName;
                            aacinfo.CreateBy = username;
                            aacinfo.ModifyBy = username;
                            db.AACInfoes.Add(aacinfo);
                            db.SaveChanges();
                        }
                    }
                    return "上传成功";
                }
                return "此专辑已存在";
            }
            return "上传失败";
        }

        public ActionResult SongsListTmpl(string s)
        {
            ViewBag.QueryString = s;
            return PartialView();
        }

        public ActionResult SongsList(string g,int PageIndex)
        {
            PageList<AACInfo> mode = new PageList<AACInfo>(db.AACInfoes.Where(o => o.Name.Contains(g)).OrderByDescending(o => o.CreateDate), PageIndex, 20);
            return Json(mode, JsonRequestBehavior.AllowGet);
        }

        public ActionResult AlbumsTmpl(string s)
        {
            ViewBag.QueryString = s;
            return PartialView();
        }

        public ActionResult AlbumsList(string g, int PageIndex)
        {
            PageList<Album> mode = new PageList<Album>(db.Albums.Where(o => o.AlbumName.Contains(g)).OrderByDescending(o => o.CreateDate), PageIndex, 10);
            return Json(mode, JsonRequestBehavior.AllowGet);
        }
    }
}
